package com.wk.colorprogressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

public class ColorProgressViewHorizontal extends View {
    private float maxValue = 100;
    private float currentValue;
    private int mWidth, mHeight;
    private int padding  =2 ;
    private RectF rectFBackGround ;
    private RectF rectFBackGround2 ;
    private RectF rectFLevel_1 ;
    private Paint mPaint;

    public ColorProgressViewHorizontal(Context context) {
        super(context);
        initView();
    }

    public ColorProgressViewHorizontal(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ColorProgressViewHorizontal(Context context, AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rectFBackGround =new RectF(0, 0, mWidth, mHeight);
                rectFBackGround2 = new RectF(padding, padding, mWidth - padding, mHeight - padding);
                rectFLevel_1 = new RectF(0, 0,  0   , 0);
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        } );
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(15);
        } else {
            mHeight = heightSpecSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int round = mHeight / 2;

        mPaint.setColor(Color.parseColor("#168cfa"));
        canvas.drawRoundRect(rectFBackGround, round, round, mPaint);

        mPaint.setColor(Color.GRAY);
        canvas.drawRoundRect(rectFBackGround2, round, round, mPaint);

        // 当前进度百分比
        float section = currentValue / maxValue;
        if(section>= 0.7){
            mPaint.setColor(Color.GREEN);
        }

        else if(section>=0.3){
            mPaint.setColor(Color.parseColor("#ff8000"));
        }

        else if(section>0) {
            mPaint.setColor(Color.RED);
        }

        else{
            mPaint.setColor(Color.TRANSPARENT);
        }

        rectFLevel_1.set(padding, padding, (mWidth - padding) * section, mHeight - padding);
        canvas.drawRoundRect(rectFLevel_1, round, round, mPaint);
    }

    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    public void setMaxCount(float maxCount) {
        this.maxValue = maxCount;
    }

    public void setCurrentCount(float value) {
        this.currentValue = value > maxValue ? maxValue : value;
        invalidate();
    }
}
